package com.olocat.cjx.nsims.dao

import com.olocat.cjx.nsims.pojo.personnel.User
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import javax.transaction.Transactional

interface UserRepository : JpaRepository<User?, String?> {
    fun findByUsernameAndPasswordEquals(username: String?, password: String?): User?
    fun findByTelephoneIs(telephone: String?): User?
    fun getUserById(id:String):User

    @Transactional
    @Modifying
    @Query("update User u set u.password=?2 where u.id=?1")
    fun updatePasswordById(id: String, password: String?)

    @Transactional
    @Modifying
    @Query("update User u set u.username=?2 where u.id=?1")
    fun updateUsernameById(id:String, username: String?)

    @Transactional
    @Modifying
    @Query("update User u set u.email=?2 where u.id=?1")
    fun updateEmailById(id: String, email: String)

    @Transactional
    @Modifying
    @Query("update User u set u.telephone=?2 where u.id=?1")
    fun updateTelephoneById(id: String, telephone: String?)

    @Transactional
    @Modifying
    @Query("update User u set u.role='nurse' where u.telephone=?1")
    fun updateRoleSetNurseByTelephone(telephone: String?)

    @Transactional
    @Modifying
    @Query("update User u set u.role='doctor' where u.telephone=?1")
    fun updateRoleSetDoctorByTelephone(telephone: String?)
}